/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.accounting.dao;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.javaeye.accounting.dto.MaterialsMonthInventoryCheck;

public class MaterialsMonthInventoryCheckDAOHibernate extends HibernateDaoSupport implements MaterialsMonthInventoryCheckDAO {
	
	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialsMonthInventoryCheck> getInventoryChecks(
			final Date beginDate, final Date endDate, final int categoryId) {
		
		HibernateCallback action = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Criteria crit = session.createCriteria(MaterialsMonthInventoryCheck.class);
				//这里为了去除重复记录
				crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
				.add(Restrictions.between("checkDate", beginDate, endDate));
				if (categoryId != -1) {
					crit.createAlias("categorys", "c")
					.add(Restrictions.eq("c.parentId", categoryId));
				}
				return crit.list();
			}
		};
		return getHibernateTemplate().executeFind(action);
	}
	
	@SuppressWarnings("unchecked")
	public int getMaxInventoryCheckId(String checkId) {
		final String queryString = "select max(ic.checkNo) from MaterialsMonthInventoryCheck ic where ic.checkNo like '" + checkId + "%'";
		List result = getHibernateTemplate().find(queryString);
		if (result.get(0) == null) {
			return 0;
		}
		String maxId = result.get(0).toString();
		maxId = maxId.replace(checkId, "");
		return Integer.parseInt(maxId);
	}
	
	public void saveInventoryCheckInfo(MaterialsMonthInventoryCheck checkInfo) {
		getHibernateTemplate().saveOrUpdate(checkInfo);
	}
	
	/**
	 * 删除盘点表
	 * @param checkNo
	 */
	public void removeInventoryCheck(String checkNo) {
		//load和get不同的是不立即执行查询
		Object o = getHibernateTemplate().load(MaterialsMonthInventoryCheck.class, checkNo);
		getHibernateTemplate().delete(o);
	}
	
	/**
	 * 查询盘点明细
	 * @param checkNo
	 */
	public MaterialsMonthInventoryCheck getInventoryCheckInfo(final String checkNo) {
		HibernateCallback action = new HibernateCallback() {
			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Object check = session.createCriteria(MaterialsMonthInventoryCheck.class)
				.setFetchMode("details", FetchMode.JOIN)
				.setFetchMode("details.skuDetail", FetchMode.JOIN)
				.setFetchMode("details.skuDetail.materials", FetchMode.JOIN)
				.add(Restrictions.idEq(checkNo))
				.uniqueResult();
				return check;
			}
		};
		return (MaterialsMonthInventoryCheck)getHibernateTemplate().execute(action);
	}
}
